package com.example.springsecuritydemo01.service;

import com.example.springsecuritydemo01.dao.UserMapper;
import com.example.springsecuritydemo01.pojo.entity.User;
import com.example.springsecuritydemo01.pojo.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据username查询用户所有信息
     * */
    public User getOne(String username){
        if (username==null)
            throw new RuntimeException("param is null");
        User user = userMapper.SelectByUsername(username);
        return user;
    }

    public UserVo getUserInfo(String username){
        UserVo userVo = new UserVo();
        if (username==null)
            throw new RuntimeException("param is null");
        User user = userMapper.SelectByUsername(username);
        BeanUtils.copyProperties(user,userVo);
        List<Integer> roleIds = userMapper.selectRoleIdByUserId(user.getId());
        List<String> roles = userMapper.selectRoleByIds(roleIds);
        userVo.setRoles(roles);
        List<Integer> powerIds=new ArrayList<>();
        for (Integer id:roleIds){
            List<Integer> powerId = userMapper.selectPowerIdByRoleId(id);
            powerIds.addAll(powerId);
        }
        List<String> powers = userMapper.selectPowerByIds(powerIds);
        userVo.setPowers(powers);
        return userVo;
    }
}
